package com.artfii.fluentsql.example;

import com.artfii.fluentsql.core.mock.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class TestFluentSqlController {
    Logger logger = LoggerFactory.getLogger(TestFluentSqlController.class);

    @Resource
    private TestUserService testUserService;


    @RequestMapping("")
    @ResponseBody
    public String index(){
        return "Yes!Fluentsql springboot demo start success!";
    }

    @GetMapping("/test/fluentsql/create/table")
    @ResponseBody
    public String createTable() {
        Integer rows = testUserService.testCreateTable();
        return rows==0?"创建成功":"创建失败";
    }

    @RequestMapping("/test/fluentsql/user")
    @ResponseBody
    public List<User> getUserAll(){
        List<User> userList=testUserService.getAll();
        return userList;
    }

    @RequestMapping("/test/fluentsql/user/{id}")
    @ResponseBody
    public User getUser(@PathVariable Integer id){
        User rUser = testUserService.getUserById(id);
        logger.info("Read User data:{}",rUser);
        return rUser==null?new User():rUser;
    }

    @RequestMapping("/test/fluentsql/addUser")
    @ResponseBody
    public String addUser() {
        return testUserService.addUser();
    }

    @RequestMapping("/test/fluentsql/update/{id}")
    @ResponseBody
    public String update(@PathVariable Integer id) {
        Integer rows = testUserService.update(id);
        return "更新成功，笔数：：" + rows;
    }

    @RequestMapping("/test/fluentsql/addjdbc")
    @ResponseBody
    public String addofjdbc() {
        testUserService.addOfJdbc();
        return "Ok";
    }

    @RequestMapping("/test/fluentsql/updatejdbc/{id}")
    @ResponseBody
    public String updatejdbc(@PathVariable Integer id) {
        Integer rows = testUserService.updateOfJdbc(id);
        return "更新成功，笔数：：" + rows;
    }


    /**
     * 批量写入
     * @return
     */
    @RequestMapping("/test/fluentsql/batchsave")
    @ResponseBody
    public String batchSave() {
        Integer rows = testUserService.batchSave();
        return "批量更新，笔数：：" + rows;
    }

    @RequestMapping("/test/fluentsql/batchupdate")
    @ResponseBody
    public String batchUpdate() {
        Integer rows = testUserService.batchUpdate();
        return "Batch update，nums：" + rows;
    }

}
